<?php
namespace Home\Controller;

use Think\Controller;
class BankController extends Controller {
	private $TRANS_URL="http://app.chinaxzf.com:3386/index.unifypay?action=MemberTransaction";
	private $MERID="834241045820001";
	private $SHOPID="PT1383";
	private $KEY="F5CF87D180E0B9D773C70AD2D0E513F5";
	
	
     
    public function test(){
    	header ( "Content-Type: text/html;charset=utf-8" );
    	$partnerCode = 'PT1383';
    	$merId = '834241057320001';
    	$security_key = 'F5CF87D180E0B9D773C70AD2D0E513F5';
    	$acctName=I('acctName');
    	$cerdId=I('cerdId');
    	$token=I('token');
    	$where1['token']=$token;
    	$member_id=M('token')->where($where1)->find()['uid'];
    	
    	$arr['member_id']=$member_id;
    	$arr['money']=I('price');
    	$price=100*I('price');
    	$arr['status']=0;
    	$arr['type']=2;
    	$arr['add_time']=time();
    	$data =[  
			    "orderNo"=>$member_id.time(),
			    "bizcode"=> "3105",
			    "memberNo"=> $this->MERID,
			    "transAmt"=> $price,
			    "notifyUrl"=> "http://dz.money654.com/Home/Return/paybybank",
			    //"showUrl"=> "http://dz.money654.com/zlt/www",
				"acctName"=> $acctName,
				"cerdId"=> $cerdId
 		]; 
    	$arr['order_num']=$data['orderNo'];
    	$r=M('Pay')->add($arr);
//     	$params ['memberNo'] = $merId;
//     	$params ['bizList'] = array (array ('bizcode' => '1003','rate' => '0.45' ),array ('bizcode' => '2003','rate' => '0.45' ) );
    	$jsonData = json_encode ( $data );
    	
    	$dt = 'partnerCode=' . $partnerCode . '&encryptData=' . base64_encode ( $jsonData ) . '&signData=' . $this->sign ( $jsonData, $security_key );
    	
    	$result = $this->post2 ( $this->TRANS_URL, $dt );
    	
    	$result1=explode('&', $result);
    	$result2=base64_decode(substr($result1[1],11));
    	if($result2){
		$a['info']=$result2;
		$a['status']=1;
		$this->ajaxReturn($a,'JSONP');
    	}else{
    		$a['info']='';
    		$a['status']=0;
    		$this->ajaxReturn($a,'JSONP');
    	}
    }
    
    
    private function sign($jsonData, $security_key) {
    	ini_set('date.timezone','Asia/Shanghai');
    	return  md5 ( $jsonData .  $security_key );
    }
    
    
    private static function post2($url, $data) {
    	if (is_array($data))
    		$postdata = http_build_query($data);
    	else
    		$postdata = $data;
    	$opts = array('http' =>
    			array(
    					'method'  => 'POST',
    					'header'  => 'Content-type: application/x-www-form-urlencodedrn;charset=utf-8',
    					'content' => $postdata
    			)
    	);
    	$context = stream_context_create($opts);
    	$result = file_get_contents($url, false, $context);
    	return  $result;
    }
    
}